﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.Security;


namespace eStore.UserControl
{
    public partial class RegisterUI : System.Web.UI.UserControl
    {
        private eStoreModelDataContext db = new eStoreModelDataContext();
        private const string STR_RegisterUI = "RegisterUI";//package ngon ngu su dung
        protected string UpdateProgressRegisterText = String.Empty;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// 
        /// //Summary
        /// //
        /// //Summary
        /// //
        /// //Project
        /// //eStoteSolution
        /// //Project
        /// ============================Coding Information=============================
        /// Coding By : QUAN TRUONG TRONG
        /// Date Create : 6/26/2010
        /// Time Create : 2:56 AM
        /// ===========================================================================
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page_LoadResources();
            }       
        }

        private void Page_LoadResources()
        {
            LegendRegister.InnerText = (string)GetGlobalResourceObject(STR_RegisterUI, "LegendRegister");
            LegendInfo.InnerText = (string)GetGlobalResourceObject(STR_RegisterUI, "LegendInfo");
            h2CreateAcc.InnerText = (string)GetGlobalResourceObject(STR_RegisterUI, "h2CreateAcc");
            pAccInfoUse.InnerText = (string)GetGlobalResourceObject(STR_RegisterUI, "pAccInfoUse");
            UpdateProgressRegisterText = (string)GetGlobalResourceObject(STR_RegisterUI, "UpdateProgressRegisterText");

            //load text for control
            LabelUserName.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelUserName");
            LabelPassword.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelPassword");
            LabelPasswordConfirm.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelPasswordConfirm");
            LabelEmail.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelEmail");
            LabelFullName.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelFullName");
            LabelBirthYear.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelBirthYear");
            LabelGender.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelGender");
            LabelPhone.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelPhone");
            LabelNational.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelNational");
            LabelProvince.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelProvince");
            LabelAddress.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "LabelAddress");
            RequiredFieldValidatorUserName.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RequiredFieldValidatorUserName");
            RequiredFieldValidatorPassword.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RequiredFieldValidatorPassword");
            CompareValidatorPasswordConfirm.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "CompareValidatorPasswordConfirm");
            RegularExpressionValidatorEmail.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RegularExpressionValidatorEmail");
            RequiredFieldValidatorEmail.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RequiredFieldValidatorEmail");
            RequiredFieldValidatorFullName.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RequiredFieldValidatorFullName");
            //RequiredFieldValidatorBirthYear.Text = (string)GetGlobalResourceObject("RegisterUI", "RequiredFieldValidatorBirthYear");
            RequiredFieldValidatorAddress.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RequiredFieldValidatorAddress");
            ButtonRegister.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "ButtonRegister");
            //load wattermark
            TextBoxPhone_TextBoxWatermarkExtender.WatermarkText = (string)GetGlobalResourceObject(STR_RegisterUI, "TextBoxPhone_TextBoxWatermarkExtender");
            //TextBoxUserName_TextBoxWatermarkExtender.WatermarkText = (string)GetGlobalResourceObject("RegisterUI", "TextBoxUserName_TextBoxWatermarkExtender");
            //TextBoxPassword_TextBoxWatermarkExtender.WatermarkText = (string)GetGlobalResourceObject("RegisterUI", "TextBoxPassword_TextBoxWatermarkExtender");
            //Radio Gender
            ListItem RadioButtonMale = new ListItem();
            RadioButtonMale.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RadioButtonMale");
            RadioButtonMale.Selected = true;
            ListItem RadioButtonFerMale = new ListItem();
            RadioButtonFerMale.Text = (string)GetGlobalResourceObject(STR_RegisterUI, "RadioButtonFerMale");
            RadioButtonListGender.Items.Add(RadioButtonMale);
            RadioButtonListGender.Items.Add(RadioButtonFerMale);
            //load du lieu cac quoc gia
            DropDownListNational.DataSource = db.eStore_Nationals.OrderBy(c => c.NationalName); ;
            DropDownListNational.DataTextField = "NationalName";
            DropDownListNational.DataValueField = "NationalName";
            DropDownListNational.DataBind();
            DropDownListNational.Text = "Viet Nam";
            ShowProvince();
            //load nam sinh
            for (int i = 1900; i < 2000; i++)
            {
                ListItem item = new ListItem();
                item.Text = i.ToString();
                DropDownListBirthYear.Items.Add(item);
            }
            DropDownListBirthYear.SelectedValue = "1988";
        }

        protected void ButtonRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Membership.CreateUser(TextBoxUserName.Text, TextBoxPassword.Text, TextBoxEmail.Text);
                    //them moi thong tin nguoi dung
                    eStore_UserInfo UserInfo = new eStore_UserInfo();
                    UserInfo.UserName = TextBoxUserName.Text;
                    UserInfo.FullName = TextBoxFullName.Text;
                    UserInfo.BirthYear = DropDownListBirthYear.SelectedValue;
                    UserInfo.Gender = RadioButtonListGender.SelectedValue;
                    UserInfo.Phone = TextBoxPhone.Text;
                    UserInfo.Province = DropDownListProvince.Text;
                    UserInfo.NationalName = DropDownListNational.Text;
                    UserInfo.Address = TextBoxAddress.Text;
                    db.eStore_UserInfos.InsertOnSubmit(UserInfo);
                    db.SubmitChanges();
                    Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageSuccess&&DetailsHeader=DetailsHeaderSuccess&&DetailsText=DetailsTextSuccess&&btnBack=btnBackSuccess&&urlBack=" + Request.Url.ToString());
                }
                catch (MembershipCreateUserException ex)
                {
                    switch (ex.StatusCode)
                    {
                        case MembershipCreateStatus.InvalidPassword:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextInvalidPasswordError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                        case MembershipCreateStatus.InvalidEmail:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextInvalidEmailError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                        case MembershipCreateStatus.DuplicateUserName:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextDuplicateUserNameError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                        case MembershipCreateStatus.DuplicateEmail:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextDuplicateEmailError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                        case MembershipCreateStatus.UserRejected:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextUserRejectedError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                        default:
                            Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_RegisterUI + "&&header=h2CreateAcc" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextDefaultError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                            break;
                    }
                }

           }
        }

        protected void DropDownListNational_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowProvince();        
        }
        private void ShowProvince()
        {
            if (DropDownListNational.Text.Equals("Viet Nam"))
            {
                DropDownListProvince.Enabled = true;
                DropDownListProvince.DataSource = db.eStore_Provinces.Where(c => c.National == DropDownListNational.Text).OrderBy(c=>c.ProvinceName);
                DropDownListProvince.DataTextField = "ProvinceName";
                DropDownListProvince.DataValueField = "ProvinceName";
                DropDownListProvince.DataBind();
            }
            else
            {
                DropDownListProvince.Items.Clear();
                DropDownListProvince.Enabled = false;
            }
        }

        protected void TextBoxUserName_TextChanged(object sender, EventArgs e)
        {
           // System.Threading.Thread.Sleep(5000);  
        }

        protected void TextBoxPassword_TextChanged(object sender, EventArgs e)
        {
            if (TextBoxPassword.Text.Length<6)
            {
                RequiredFieldValidatorPassword.Visible = true; ;
            }
        }

    }
}